package server.main.controller;

import cn.hutool.core.io.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import server.main.pojo.Result;
import server.main.service.FunctionAddSevice;
import server.main.utils.mainutils.UrlUtils;

@RestController
@RequestMapping("/funcadd")
public class FunctionAddController {

    @Autowired
    FunctionAddSevice functionAddSevice;

    @PostMapping
    public Result<String> FunctionAdd(MultipartFile file){

        String filePath = UrlUtils.get_user_dir() + "/temp/zip/";//需要更改的地方
        String flag = System.currentTimeMillis() + "";
        String fileName = file.getOriginalFilename();

        try {
            FileUtil.writeBytes(file.getBytes(), filePath + flag + "-" + fileName);
            System.out.println("******************************功能开始添加*************************************");
//            System.out.println("flag"+flag);
            System.out.println("文件上传路径"+filePath);
            System.out.println("文件名称"+flag+"-"+fileName );
            functionAddSevice.addfunc(flag+"-"+fileName);

            System.out.println("******************************功能添加完毕*************************************");

            Thread.sleep(1L);
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println(fileName + "--文件上传失败");
        }
        System.out.println(Result.success(flag));
        return Result.success(flag);

    }



}
